/**
 * Created by ZFL on 2017/11/3.
 */



var storeId= $.getPara('storeId');
var totalPrice=0;

function PageInit(){
    if($.getPara('totalPrice')==''||$.getPara('totalPrice')==0){
        //当不传金额时  需要用户自己穿金额  则显示输入金额页面
        $('.codeTopay').css({
            'display':'block'
        })
    }else{
        totalPrice=$.getPara('totalPrice');     //金额
        //生成订单
        generateOrder();
    }
}







var vm=new Vue({
    el:'#codeTopay',
    data:{
        totalPayMoney:'',
    }

})
var val='';    //.totalPayMoney的值
var totalPayDOM = $(".totalPayMoney");
$(".offerPay_number_table a").on("touchstart", function (e) {
    e.preventDefault();
    if($(this).hasClass('offerPay_number_delete')||$(this).hasClass('offer_pay_2')){
        var number='';
    }else{
        var number=$(this).html();
    }

    val = $(".totalPayMoney").val();
    //确认支付键
    if ($(this).hasClass("offer_pay_2")) {
        if(val==''){
            YDUI.dialog.toast('请输入金额',1000)
        }else if(val.indexOf('.')==0||(val.indexOf('0')==0&&val.indexOf('.')!==1)){
            YDUI.dialog.toast('金额格式不对,重新输入',1000)
        }else if(Number(val)<0.01){
            YDUI.dialog.toast('金额无效,重新输入',1000)
        }else{
            totalPrice=val*100;
            //生成订单
            generateOrder();
        }

    }


    //小数点键  (只能输一次)
    if ($(this).html() === "." && vm.totalPayMoney.indexOf(".") > -1) {
        return;
    }
    // 删除键 (删除输入的数字)
    if ($(this).hasClass("offerPay_number_delete")) {
        vm.totalPayMoney = vm.totalPayMoney.slice(0, val.length - 1);
        totalPayDOM.val(val);
        $(".totalPayMoney_gb_money").html(val);
    } else {
        val = val + $(this).html();
    }

    vm.totalPayMoney=vm.totalPayMoney+number;
});















//生成订单
function generateOrder(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.storeId=storeId;   //店铺id
            obj.totalPrice=totalPrice;
            console.log(obj)
            return obj
        },
        visibleTime: 0,
        dataType:"json",
        approve: true,
        api:'yg_bapp/orderPay/addOrderObject2',
        success:function(result) {
            if(result.code==10000){
                var data=result.data;
                //拉起支付
                if(data.result&&data.outShopId){
                  window.location.href="https://pay.qcloud.com/cpay/qrcode_disposable_payment?out_shop_id="+data.outShopId+"&total_fee="+totalPrice+"&trade_num="+data.result;
                }
            }else{

                YDUI.dialog.toast(result.message, 1000);
            }
        }
    })
}
